using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Linq;
using BUS;
using DAO;
namespace project
{
    public partial class frmquanly : DevComponents.DotNetBar.RibbonForm
    {
        NHANVIEN nv ;
        
        NhanVienBUS nvBUS =  new NhanVienBUS();
        
        public frmquanly(NHANVIEN _nv)
        {
            InitializeComponent();
            nv = _nv;
        }
        private void frmquanly_Load(object sender, EventArgs e)
        {
            loadTabXemThongTin();
            loadTabQuanlyNhanVien();
           
            
        }

        private void loadTabQuanlyNhanVien()
        {
          //  List<NHANVIEN> dsNhanVien = new List<NHANVIEN>();
           // gvDSNhanVien.AutoGenerateColumns = false;
            gvDSNhanVien.DataSource = nvBUS.LayDSNhanVien();
            cbLoaiNhanVien.DataSource = nvBUS.LayLoaiNhanVien();
            cbLoaiNhanVien.DisplayMember = "TEN_LOAI";
            cbLoaiNhanVien.ValueMember = "MA_LOAI";
            cbLoaiNhanVien.SelectedItem = null;
            cbLoaiNhanVien.Text = "";
        }

      

        private void loadTabXemThongTin()
        {
            txtHoten.Text = nv.TEN_NHAN_VIEN;
            txtMaNhanVien.Text = nv.MA_NHAN_VIEN.ToString();
            txtDiaChi.Text = nv.DIA_CHI;
            txtPhone.Text = nv.SDT.ToString();
            txtNamSinh.Text = nv.NGAY_SINH.ToString();
            txtLoaiNhanVien.Text = "Quản Lý";
          
        }

       
       
        void HideAllTab()
        {
            for (int i = 0; i < tabquanlynhanvien.Tabs.Count - 1; i++)
                tabquanlynhanvien.Tabs[i].Visible = false;

        }

        private void btniCapNhat_Click(object sender, EventArgs e)
        {
           // XuLyTextChange(sender,e);
            nv.MAT_KHAU = nv.MAT_KHAU;
            string mkc = txtMatKhauCu.Text;
            string mkm = txtMatKhauMoi.Text;
            string mknl = txtMatKhauNhapLai.Text;
            string kq = nvBUS.CapNhatMatKhau(mkc,mkm,mknl,nv);
            MessageBox.Show(kq);
            if (kq == "Cap nhat thanh cong.")
            {
                nv.MAT_KHAU = mkm;
                txtMatKhauCu.Text = "";
                txtMatKhauMoi.Text = "";
                txtMatKhauNhapLai.Text = "";
            }
        }

        void XuLyTextChange(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text.Length > 0 == true && txtMatKhauNhapLai.Text.Length > 0 == true && txtMatKhauMoi.Text.Length > 0 == true)
            {
               // btniCapNhatMatKhau.Visible = true;
                btniCapNhatMatKhau.Enabled = true;
            }
            else
            {
                btniCapNhatMatKhau.Enabled = false;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string mkc = txtMatKhauCu.Text;
            string mkm = txtMatKhauMoi.Text;
            string mknl = txtMatKhauNhapLai.Text;
            //MessageBox.Show(nv.MAT_KHAU.ToString() + " " + txtMatKhauCu.Text);
            MessageBox.Show(nv.MAT_KHAU.ToString() + " " + txtMatKhauCu.Text);
            MessageBox.Show((mkc== nv.MAT_KHAU).ToString());
        }

        private void gvDSNhanVien_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.gvDSNhanVien.SelectedCells.Count > 0 )
            {

                int dong = this.gvDSNhanVien.SelectedCells[0].RowIndex;// lay chi so dong o dc chon
                txtMa.Text = gvDSNhanVien[0, dong].Value.ToString();
                txtTen.Text = gvDSNhanVien[1, dong].Value.ToString(); 
                txtPhoneQL.Text = gvDSNhanVien[2, dong].Value.ToString(); 
                txtDiaChiQL.Text = gvDSNhanVien[3, dong].Value.ToString(); 
                dtpNgaySinhQL.Value = (DateTime) gvDSNhanVien[4, dong].Value;
                cbLoaiNhanVien.SelectedValue = int.Parse(gvDSNhanVien[5, dong].Value.ToString()) ;
                txtTaiKhoan.Text = gvDSNhanVien[6, dong].Value.ToString(); 
                txtMatKhau.Text = gvDSNhanVien[7, dong].Value.ToString(); 
            }
        }
        void LuuThongTinNhanVien()
        {
           
            nv.TEN_NHAN_VIEN = txtTen.Text;
            nv.DIA_CHI = txtDiaChiQL.Text;
            nv.NGAY_SINH = dtpNgaySinhQL.Value;
            nv.SDT = txtPhoneQL.Text;
            nv.LOAI_NHAN_VIEN = int.Parse(cbLoaiNhanVien.SelectedValue.ToString());
            nv.TAI_KHOAN = txtTaiKhoan.Text;
            nv.MAT_KHAU = txtMatKhau.Text;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            //nv.MA_NHAN_VIEN = nvInfo.MA_NHAN_VIEN;

            LuuThongTinNhanVien();
            string kq = nvBUS.ThemNhanVien(nv);
            if (kq == "Them thanh cong.")
            {
                MessageBox.Show("Them thanh cong.");
                loadTabQuanlyNhanVien();
            }
            else {
                MessageBox.Show(kq, "Lỗi....", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //MessageBox.Show(nv.TEN_NHAN_VIEN + "" + nv.DIA_CHI  + "" + nv.NGAY_SINH + "" + nv.SDT + "" + nv.LOAI_NHAN_VIEN + "" + nv.TAI_KHOAN + "" + nv.MAT_KHAU);
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (this.gvDSNhanVien.SelectedCells.Count > 0)
            {
                LuuThongTinNhanVien();
                nv.MA_NHAN_VIEN = int.Parse(txtMa.Text);
                string kq = nvBUS.CapNhatNhanVien(nv);
                loadTabQuanlyNhanVien();
                MessageBox.Show(kq);
            }
            else {
                MessageBox.Show("bạn chưa chọn nhân viên nào.");
            }
        }

        public bool KiemTraTaiKhoanTrung2(NHANVIEN nv)
        {
            QLThuVienDataContext db = new QLThuVienDataContext();
            var count = db.NHANVIENs.Count(me => me.TAI_KHOAN == nv.TAI_KHOAN && me.MA_NHAN_VIEN == nv.MA_NHAN_VIEN);
            //(from nv in db.NHANVIENs where nv.TAI_KHOAN.Trim() == p.Trim() select nv).SingleOrDefault();
            if (count == 1)
            {
                return false;
            }
            else
            {
                var count1 = db.NHANVIENs.Count(me => me.TAI_KHOAN == nv.TAI_KHOAN);
                if (count1 == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

            return true;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            //QLThuVienDataContext db = new QLThuVienDataContext();
            //string p = txtTaiKhoan.Text.Trim();
            //var count = db.NHANVIENs.Count(me => me.TAI_KHOAN.Trim() == p.Trim());
            //MessageBox.Show(KiemTraTaiKhoanTrung2(p).ToString() + " " + count.ToString());
            loadTabQuanlyNhanVien();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            LuuThongTinNhanVien();
            QLThuVienDataContext db = new QLThuVienDataContext();
            nv.MA_NHAN_VIEN = int.Parse(txtMa.Text);
            string p = txtTaiKhoan.Text.Trim();
            var count = db.NHANVIENs.Count(me => me.TAI_KHOAN == nv.TAI_KHOAN && me.MA_NHAN_VIEN == nv.MA_NHAN_VIEN);
            MessageBox.Show( KiemTraTaiKhoanTrung2(nv).ToString() + " " + count.ToString());
        }

        private void btnNghi_Click(object sender, EventArgs e)
        {
            if (this.gvDSNhanVien.SelectedCells.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc xoá nhân viên này không?",

               "Delete Warning", MessageBoxButtons.YesNo,

               MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)

               == DialogResult.Yes)
                {
                    LuuThongTinNhanVien();
                    nv.MA_NHAN_VIEN = int.Parse(txtMa.Text);
                   string kq =  nvBUS.XoaNhanVien(nv);
                   if (kq == string.Empty)
                   {
                       MessageBox.Show("Xóa thành công.");
                       loadTabQuanlyNhanVien();
                   }
                   else {
                       MessageBox.Show(kq);
                   }
                }
            }
            else
            {
                MessageBox.Show("bạn chưa chọn nhân viên nào.");
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            loadTabQuanlyNhanVien();
            txtMa.Text = "";
            txtTen.Text = "";
            txtPhoneQL.Text = "";
            txtDiaChiQL.Text = "";
            dtpNgaySinhQL.Value = DateTime.Now;
            cbLoaiNhanVien.SelectedItem = null;
            txtTaiKhoan.Text = "";
            txtMatKhau.Text = ""; 

        }
    }
}